{
    "cells":
    [
        {
            "cell_type": "markdown",
            "id": "4387798b",
            "metadata":
            {},
            "source":
            [
                "# PCA降维，聚类可视化\n",
                "\n",
                "支持2D、3D降维可视化，数据必须为numpy。并且最后一列为聚类类别。\n",
                "\n",
                "1. 2D npy文件生成，使用OKT-gen_feature_cluster。\n",
                "2. 3D npy文件生成，使用OKT-gen_habitat_cluster。\n",
                "\n",
                "使用过程中，修改path2npy路径即可。\n",
                "\n",
                "**注意**： 这个可视化底层调用的接口某些资源墙内加载可能比较慢，如果没有出现报错，请多等待，必要时候请科学上网。"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "ca3fe227",
            "metadata":
            {},
            "outputs":
            [],
            "source":
            [
                "from onekey_algo.custom.Manager import onekey_show\n",
                "\n",
                "onekey_show('模型可解释性-SHAP')"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "30e01614",
            "metadata":
            {
                "scrolled": false
            },
            "outputs":
            [],
            "source":
            [
                "from onekey_algo.custom.Manager import onekey_show\n",
                "\n",
                "onekey_show('OKT-gen_feature_cluster')"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "2ed4ac68",
            "metadata":
            {},
            "source":
            [
                "# 2D可视化"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "5147bce4",
            "metadata":
            {},
            "outputs":
            [],
            "source":
            [
                "from onekey_algo.custom.components.habitat import habitat_viz2D\n",
                "path2npy = 'E:/OnekeyDS/CT/hh/cluster_3/clusters.npy'\n",
                "habitat_viz2D(path2npy, with_voxels=True, sample_ratio=0.001)"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "3e78f4e0",
            "metadata":
            {},
            "source":
            [
                "# 3D可视化"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "72422749",
            "metadata":
            {},
            "outputs":
            [],
            "source":
            [
                "from onekey_algo.custom.components.habitat import habitat_viz\n",
                "\n",
                "path2npy = 'E:/OnekeyDS/CT/hh/cluster_3/clusters.npy'\n",
                "habitat_viz(path2npy, with_voxels=False, sample_ratio=0.1)"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "84d7c11f",
            "metadata":
            {},
            "source":
            [
                "# t-SNE降维可视化"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "8a4d06b9",
            "metadata":
            {},
            "outputs":
            [],
            "source":
            [
                "import pandas as pd\n",
                "from onekey_algo.custom.components.comp1 import analysis_features\n",
                "\n",
                "feature_file = r'E:/OnekeyDS/CT/clinical.csv'\n",
                "label_file = r'E:/OnekeyDS/CT/label.csv'\n",
                "\n",
                "feature_data = pd.read_csv(feature_file)\n",
                "label_data = pd.read_csv(label_file)\n",
                "data = pd.merge(feature_data, label_data, on='ID', how='inner')\n",
                "analysis_features(data[[c for c in data.columns if c != 'label']], data['label'], save_dir='img', legend=['balabala', 'xiaomoxian', 'bbxx'])"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "50111adb",
            "metadata":
            {},
            "source":
            [
                "# 相关系数热图"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "ce9584ef",
            "metadata":
            {},
            "outputs":
            [],
            "source":
            [
                "import os\n",
                "import seaborn as sns\n",
                "import matplotlib.pyplot as plt\n",
                "from onekey_algo.custom.components.comp1 import draw_matrix\n",
                "plt.figure(figsize=(12.0, 12.0))\n",
                "\n",
                "feature_file = r'E:/OnekeyDS/CT/clinical.csv'\n",
                "feature_data = pd.read_csv(feature_file)\n",
                "os.makedirs('img', exist_ok=True)\n",
                "# 选择可视化的相关系数\n",
                "draw_matrix(feature_data.corr('pearson'), annot=True, cmap='YlGnBu', cbar=False)\n",
                "plt.savefig(f'img/feature_corr.svg', bbox_inches = 'tight')"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "b0943012",
            "metadata":
            {},
            "source":
            [
                "# 样本聚类"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "6ee6560e",
            "metadata":
            {},
            "outputs":
            [],
            "source":
            [
                "import seaborn as sns\n",
                "import pandas as pd\n",
                "import numpy as np\n",
                "import matplotlib.pyplot as plt\n",
                "\n",
                "feature_file = r'E:/OnekeyDS/CT/clinical.csv'\n",
                "feature_data = pd.read_csv(feature_file)\n",
                "\n",
                "pp = sns.clustermap(feature_data[[c for c in feature_data if np.int8 <= feature_data[c].dtype <= np.float64]].T, \n",
                "                    linewidths=.5, figsize=(10.0, 12.0), cmap='jet', z_score=0)\n",
                "plt.setp(pp.ax_heatmap.get_yticklabels(), rotation=90)\n",
                "plt.savefig(f'img/feature_cluster.svg', bbox_inches = 'tight')"
            ]
        },
        {
            "cell_type": "markdown",
            "id": "6ae6b5d4",
            "metadata":
            {},
            "source":
            [
                "# 其他可视化\n",
                "\n",
                "如果是病理WSI的聚类结果可视化，或者是预测结果可视化，可以使用OKT-gen_probably_map"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "44aacb89",
            "metadata":
            {},
            "outputs":
            [],
            "source":
            [
                "from onekey_algo.custom.Manager import onekey_show\n",
                "\n",
                "onekey_show('OKT-gen_probably_map')"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "id": "c6b4fc94",
            "metadata":
            {},
            "outputs":
            [],
            "source":
            []
        }
    ],
    "metadata":
    {
        "kernelspec":
        {
            "display_name": "Python 3 (ipykernel)",
            "language": "python",
            "name": "python3"
        },
        "language_info":
        {
            "codemirror_mode":
            {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.7.12"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 5
}